package com.lzq.model.factory.abs;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/15 14:13
 * @updateUser : modify
 * @updateTime : 2022/11/15 14:13
 * @updateRemark : 静态工厂  读取配置文件
 */
public class CoffeeStaticFactory {
    private static Map<String, Coffee> map = new HashMap();

    static {
        Properties properties = new Properties();
        InputStream resourceAsStream = CoffeeStaticFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            properties.load(resourceAsStream);
            Set<Object> keys = properties.keySet();
            for (Object key : keys) {
                String property = properties.getProperty((String) key);
                // 通过反射创建对象
                Class clazz = Class.forName(property);
                Coffee coffee = (Coffee) clazz.newInstance();
                map.put((String) key, coffee);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Coffee createCoffee(String name) {
        return map.get(name);
    }
}

class TestProperties {
    public static void main(String[] args) {
        Coffee usa = CoffeeStaticFactory.createCoffee("usa");
        System.out.println(usa.getName());
    }
}